package com.caldremch.sophixdemo;

import android.content.Context;
import android.support.annotation.Keep;
import android.util.Log;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 * @Author Caldremch
 * @Date 2017/9/13 16:28
 * @Email huang.zhongxian@zyxr.com
 * @Description
 **/
public class SophixStubApplication extends SophixApplication {

    public interface MsgDisplayListener {
        void handle(String msg);
    }
    public static MsgDisplayListener msgDisplayListener = null;
    public static StringBuilder cacheMsg = new StringBuilder();

    private final String TAG = "SophixStubApplication";
    // 此处SophixEntry应指定真正的Application，并且保证RealApplicationStub类名不被混淆。
    @Keep
    @SophixEntry(SophixApp.class)
    static class RealApplicationStub {}


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//         如果需要使用MultiDex，需要在此处调用。
//         MultiDex.install(this);
        initSophix();
    }
    private void initSophix() {
        String appVersion = BuildConfig.VERSION_NAME;
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setAppVersion(appVersion)
                .setSecretMetaData("24797575", "1d5f2f00fe5094cd47b4ebcae22a369f", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeZj96NR+wxNW0vr6n9Y+Xnva60oUYXY6eTAWkOXGpCtpSxB8Ae0OOGO7RnFnW5vkn2qd5sCm9KbHcZBcT3M3/62QxyXQV4VIdKWvo2CsUwsR3iG4u86LsPLJNpI9nnyCtdXCJzdn0qYLAt1UcImrla3nt6PUOh1TXaCfAXTEZq82EGT1Hi3R1WFTSqTdjfqmVWRhSWHERAb3ia5EbSh7OVuO5Kdic73ya8muYAVnFe22koFZaHIgpfcGBEADMuSNjCbVeJwgiubLvNFU0NV5vnrsBdswzPbeIUL1bo2vCnLaYd1YAtz4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IJMvvdbGeExcMPqIk4nERAoGADJhKoVA5qPWXvaJf4boKjlcez3jRuJpF/aikXL31xItX85lzbcM9nrypOfBELwTyMZNNEYWmMdCyMTL7JYVasd0SeMv+Icqmo78LXHsm39optTj2vcisA4AZLgISa+5hnZf0L1HPIyUUwOnToWs+4BHiUslkwTliJN4kAaGHunE=")
                .setEnableDebug(true)
                .setEnableFullLog()
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {

                        String msg = new StringBuilder("").append("Mode:").append(mode)
                                .append(" Code:").append(code)
                                .append(" Info:").append(info)
                                .append(" HandlePatchVersion:").append(handlePatchVersion).toString();
                        if (msgDisplayListener != null) {
                            msgDisplayListener.handle(msg);
                        } else {
                            cacheMsg.append("\n").append(msg);
                        }


//                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
//                            Log.i(TAG, "sophix load patch success!");
//                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
//                            // 如果需要在后台重启，建议此处用SharePreference保存状态。
//                            Log.i(TAG, "sophix preload patch success. restart app to make effect.");
//                        }
                    }
                }).initialize();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // queryAndLoadNewPatch不可放在attachBaseContext 中，否则无网络权限，建议放在后面任意时刻，如onCreate中
        SophixManager.getInstance().queryAndLoadNewPatch();
        /** 补丁在后台发布之后, 并不会主动下行推送到客户端, 客户端通过调用queryAndLoadNewPatch方法查询后台补丁是否可用*/

    }
}
